package close.mazegame.input;

import close.mazegame.input.GamepadMap.Model;

import com.badlogic.gdx.controllers.Controller;
import com.badlogic.gdx.controllers.ControllerListener;
import com.badlogic.gdx.controllers.Controllers;
import com.badlogic.gdx.controllers.PovDirection;
import com.badlogic.gdx.math.Vector3;

public abstract class PadInput<T extends Enum<T>> extends BasicInput<T> implements ControllerListener {
	
	protected final GamepadMap buttonMap;
	protected float left_stick_x = 0.0f;
	protected float left_stick_y = 0.0f;
	protected float right_stick_x = 0.0f;
	protected float right_stick_y = 0.0f;
	protected float left_trigger = 0.0f;
	protected float right_trigger = 0.0f;

	public PadInput(Model model) {
		buttonMap = new GamepadMap(model);
		Controllers.addListener(this);
	}
	
	@Override
	public void dispose() {
		Controllers.removeListener(this);
	}

	@Override
	public void connected(Controller controller) {
		System.out.println(controller.getName() + " connected");
	}

	@Override
	public void disconnected(Controller controller) {
		System.out.println(controller.getName() + " disconnected");
	}

	@Override
	public boolean buttonDown(Controller controller, int buttonCode) {
		pressKey(buttonCode);
		return false;
	}

	@Override
	public boolean buttonUp(Controller controller, int buttonCode) {
		releaseKey(buttonCode);
		return false;
	}

	@Override
	public boolean axisMoved(Controller controller, int axisCode, float value) {
		if (axisCode == buttonMap.AXIS_LEFT_X) {
			left_stick_x = value;
			return true;
		}
		
		if (axisCode == buttonMap.AXIS_LEFT_Y) {
			left_stick_y = value;
			return true;
		}
		
		if (axisCode == buttonMap.AXIS_RIGHT_X) {
			right_stick_x = value;
			return true;
		}
		
		if (axisCode == buttonMap.AXIS_RIGHT_Y) {
			right_stick_y = value;
			return true;
		}
		
		if (axisCode == buttonMap.AXIS_LEFT_TRIGGER) {
			left_trigger = value;
			return true;
		}
		
		if (axisCode == buttonMap.AXIS_RIGHT_TRIGGER) {
			right_trigger = value;
			return true;
		}
		
		return false;
	}

	@Override
	public boolean povMoved(Controller controller, int povCode, PovDirection value) {
		return false;
	}

	@Override
	public boolean xSliderMoved(Controller controller, int sliderCode, boolean value) {
		return false;
	}

	@Override
	public boolean ySliderMoved(Controller controller, int sliderCode, boolean value) {
		return false;
	}

	@Override
	public boolean accelerometerMoved(Controller controller, int accelerometerCode, Vector3 value) {
		return false;
	}
	
}
